<script setup lang="ts">
import { computed, ref } from 'vue';
import { getCategoryDataAPI } from '@/apis/category';
import type { CategoryPrimaryItem } from '@/types/category';
import type { BannerItem } from '@/types/home';
import { getHomeBannerAPI } from '@/apis/home';
import { onLoad } from '@dcloudio/uni-app';
import CategorySkeleton from './category.skeleton.vue';

// 轮播图列表
const categoryBannerList = ref<BannerItem[]>();
const getCategoryBanner = async () => {
  const response = await getHomeBannerAPI(2).request;
  categoryBannerList.value = response.result;
};

// 分类列表数据
const categoryPrimaryList = ref<CategoryPrimaryItem[]>();
const getCategoryData = async () => {
  const response = await getCategoryDataAPI().request;
  categoryPrimaryList.value = response.result;
};

// 当前选中一级分类下标
const activeCategoryPrimaryIndex = ref(0);

// 当前选中一级分类数据
const currentCategoryPrimary = computed(() => categoryPrimaryList.value?.[activeCategoryPrimaryIndex.value]);

onLoad(async () => {
  try {
    await Promise.all([getCategoryBanner(), getCategoryData()]);
  }
  catch (e) {
    uni.showToast({ title: '当前网络状况不佳，请稍后重试！', icon: 'none' });
  }
});
</script>

<template>
  <view class="viewport" v-if="categoryBannerList && categoryPrimaryList">
    <!-- 搜索框 -->
    <view class=search>
      <view class="input">
        <text class="icon-search">请输入商品名称</text>
      </view>
    </view>
    <!-- 分类 -->
    <view class="categories">
      <!-- 左侧一级分类 -->
      <scroll-view class="primary" scroll-y :show-scrollbar="false">
        <view class="item" v-for="(item, index) in categoryPrimaryList" :key="item.id"
          :class="{ active: index === activeCategoryPrimaryIndex }" @tap="activeCategoryPrimaryIndex = index">
          <text class="name">{{ item.name }}</text>
        </view>
      </scroll-view>
      <!-- 右侧二级分类 -->
      <scroll-view class="secondary" scroll-y :show-scrollbar="false">
        <!-- 焦点图 -->
        <XtxSwiper class="banner" :bannerList="categoryBannerList" />
        <!-- 内容区域 -->
        <view class="panel" v-for="item in currentCategoryPrimary?.children" :key="item.id">
          <!-- 分类标题 -->
          <view class="title">
            <text class="name">{{ item.name }}</text>
            <navigator class="more" hover-class="none" url="">查看更多</navigator>
          </view>
          <view class="section">
            <!-- 商品项 -->
            <navigator class="goods" hover-class="none" v-for="good in item.goods" :key="good.id"
              :url="`/pages/good/good?id=${good.id}`">
              <image class="image" :src="good.picture" />
              <view class="name ellipsis">{{ good.name }}</view>
              <view class="price">
                <text class="symbol">￥</text>
                <text class="number">{{ good.price }}</text>
              </view>
            </navigator>
          </view>
        </view>
      </scroll-view>
    </view>
  </view>
  <CategorySkeleton v-else />
</template>

<style lang="scss">
.viewport {
  height: 100vh;
  display: flex;
  flex-direction: column;
}

.search {
  padding: 20rpx 30rpx;
  background-color: #fff;

  .input {
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 64rpx;
    padding-left: 26rpx;
    color: #8b8b8b;
    font-size: 28rpx;
    border-radius: 32rpx;
    background-color: #f3f4f4;
  }
}

.icon-search {
  &::before {
    margin-right: 10rpx;
  }
}

/* 分类 */
.categories {
  flex: 1;
  min-height: 400rpx;
  display: flex;
}

/* 一级分类 */
.primary {
  overflow: hidden;
  width: 180rpx;
  flex: none;
  background-color: #f6f6f6;

  .item {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 96rpx;
    font-size: 26rpx;
    color: #595c63;
    position: relative;

    &::after {
      content: '';
      position: absolute;
      left: 42rpx;
      bottom: 0;
      width: 96rpx;
      border-top: 1rpx solid #e3e4e7;
    }
  }

  .active {
    background-color: #fff;

    &::before {
      content: '';
      position: absolute;
      left: 0;
      top: 0;
      width: 8rpx;
      height: 100%;
      background-color: #27ba9b;
    }
  }
}

.primary .item:last-child::after,
.primary .active::after {
  display: none;
}

/* 二级分类 */
.secondary {
  background-color: #fff;

  .carousel {
    height: 200rpx;
    border-radius: 4rpx;
    overflow: hidden;
  }

  .panel {
    margin: 0 30rpx 0rpx;
  }

  .title {
    height: 60rpx;
    line-height: 60rpx;
    color: #333;
    font-size: 28rpx;
    border-bottom: 1rpx solid #f7f7f8;

    .more {
      float: right;
      padding-left: 20rpx;
      font-size: 24rpx;
      color: #999;
    }
  }

  .more {
    &::after {
      font-family: 'erabbit' !important;
      content: '\e6c2';
    }
  }

  .section {
    width: 100%;
    display: flex;
    flex-wrap: wrap;
    padding: 20rpx 0;

    .goods {
      width: 150rpx;
      margin: 0rpx 30rpx 20rpx 0;

      &:nth-child(3n) {
        margin-right: 0;
      }

      image {
        width: 150rpx;
        height: 150rpx;
      }

      .name {
        padding: 5rpx;
        font-size: 22rpx;
        color: #333;
      }

      .price {
        padding: 5rpx;
        font-size: 18rpx;
        color: #cf4444;
      }

      .number {
        font-size: 24rpx;
        margin-left: 2rpx;
      }
    }
  }
}
</style>
